import java.util.*;
import java.util.stream.Collectors;

public class 全排列 {
    static int sum = 0;

    public static List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        Arrays.sort(nums);
        HashMap<Integer,Integer>map=new HashMap<>();
        for(int n:nums)
            map.put(n,map.getOrDefault(n,0)+1);
        s(nums, res, new ArrayList<>(), 0, nums.length,map);

        System.out.println(res);
        return res;
    }

    public static void s(int[] q, List<List<Integer>> res, ArrayList<Integer> nums, int dep, int n,HashMap<Integer,Integer>map) {
        if (dep == n) {
            List<Integer> t = (List<Integer>) nums.clone();
            List<Integer> collect = t.stream().map(a -> q[a]).collect(Collectors.toList());
            res.add(collect);
            sum++;
            return;
        }
        for (int i = 0; i < n; i++) {
            if ((i > 0 && q[i] == q[i - 1]) )
                continue;
            if(nums.contains(i)){
                int s=0;
                for(int x:nums){
                    if(x==i)
                        s++;
                }
                if(s>=map.get(q[i]))
                    continue;
            }
            nums.add(i);
            s(q, res, nums, dep + 1, n,map);
            nums.remove(dep);
        }

    }
    public static String getPermutation(int n, int k) {
        StringBuffer res=new StringBuffer();
        int jie_cheng=1;
        int wei=1;
        for(;wei<n;wei++){
            jie_cheng*=wei;
            if(jie_cheng>=k)
                break;
        }
        for(int i=0;i<n-wei;i++)
          ;


        return res.toString();
    }
    public static void main(String[] args) {
        List<int[]> res = new ArrayList<>();
        int[] nums = new int[]{1,1,2};
        permute(nums);
        System.out.println(sum);

    }
}
